package edu.task.controller;

import edu.task.common.base.BaseController;
import edu.task.common.constant.UserConstants;
import edu.task.common.result.RestResult;
import edu.task.common.result.TableDataInfo;
import edu.task.model.SysRole;
import edu.task.model.vo.SysRoleVO;
import edu.task.model.vo.SysUserVO;
import edu.task.service.SysMenuService;
import edu.task.service.SysRoleService;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色表控制层
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {
    @Autowired
    SysRoleService roleService;

    @Autowired
    SysMenuService menuService;

    /**
     * 显示角色表所有数据
     */
    @GetMapping("/list")
    public RestResult list() {
        RestResult ajax = RestResult.success();
        ajax.put(RestResult.DATA_TAG, roleService.selectRoleList());
        return ajax;
    }

    /**
     * 角色新增
     */
    @PostMapping
    public RestResult add(@RequestBody SysRoleVO role) {
        boolean flag = roleService.insertUser(role);
        return toAjax(flag ? 1 : 0);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleIds}")
    public RestResult remove(@PathVariable Long[] roleIds){
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 修改新增
     */
    @PutMapping
    public RestResult edit(@Validated @RequestBody SysRoleVO role) {
        boolean flag = roleService.updateRole(role);
        return toAjax(flag ? 1 : 0);
    }


    /**
     * 根据角色编号获取角色列表信息
     */
    @GetMapping(value = {"/","/{roleId}"})
    public RestResult getInfo(@PathVariable(value = "roleId",required = false) Long roleId){
        RestResult ajax = RestResult.success();
        if (StringUtils.isNotNull(roleId)){
            ajax.put(RestResult.DATA_TAG, roleService.selectRoleById(roleId));
            ajax.put("menuIds", menuService.selectMenuListByRoleId(roleId));
        }
        return ajax;
    }


}
